package org.study.neo4j.boot.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.neo4j.driver.internal.InternalPoint2D;
import org.neo4j.driver.types.Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import org.springframework.stereotype.Service;
import org.study.neo4j.boot.dto.ActorDTO;
import org.study.neo4j.boot.model.*;
import org.study.neo4j.boot.repository.MovieRepository;

import java.util.*;

/**
 * 参考自 https://huaweicloud.csdn.net/63876f2ddacf622b8df8c202.html
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年04月14日
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Service
public class MovieService {
    private final MovieRepository movieRepository;
    private final Neo4jClient neo4jClient;
    private final Neo4jTemplate neo4jTemplate;

    public void addData() {
        Movie movie = new Movie("我爱我的祖国", "该片讲述了新中国成立70年间普通百姓与共和国息息相关的故事");

        List<MovieActorRelationship> movieActorRelationshipList = new ArrayList<>(8);
        movieActorRelationshipList.add(new MovieActorRelationship(new Actor("黄渤", 1974, new Point(40.220952, 116.231034)), List.of("林治远")));
        movieActorRelationshipList.add(new MovieActorRelationship(new Actor("张译", 1978), List.of("高远")));
        movieActorRelationshipList.add(new MovieActorRelationship(new Actor("吴京", 1974, new Point(39.904179, 116.407387)), List.of("陈冬冬")));
        movieActorRelationshipList.add(new MovieActorRelationship(new Actor("杜江", 1985), List.of("朱涛")));
        movieActorRelationshipList.add(new MovieActorRelationship(new Actor("葛优", 1957, new Point(31.221461, 121.544346)), List.of("张北京")));
        movieActorRelationshipList.add(new MovieActorRelationship(new Actor("刘昊然", 1997), List.of("沃德乐")));

        movie.setMovieActorRelationshipList(movieActorRelationshipList);
        movie.setDirectorList(Collections.singletonList(new Actor("陈凯歌", 1952)));

        movieRepository.save(movie);
    }

    public Movie findMovieByRepository() {
        return movieRepository.findById("我爱我的祖国").orElse(null);
    }

    public Movie findMovieByTemplate() {
        return neo4jTemplate.findById("我爱我的祖国", Movie.class).orElse(null);
    }


    public void deleteAllMovie() {
        movieRepository.deleteAll();
    }

    public void deleteAllActor() {
        movieRepository.deleteAllActor();
    }

    public Collection<ActorDTO> getAllActor() {
        Collection<ActorDTO> actorCollection = neo4jClient.query("MATCH (p:Actor) RETURN p")
                .fetchAs(ActorDTO.class)
                .mappedBy((typeSystem, record) -> {
                    ActorDTO actorDTO = new ActorDTO();
                    Node node = record.get(0).asNode();
                    Map<String, Object> map = node.asMap();
                    Actor actor = BeanUtil.toBeanIgnoreError(map, Actor.class);
                    BeanUtil.copyProperties(actor, actorDTO);
                    String first = CollUtil.getFirst(node.labels());
                    InternalPoint2D internalPoint2D = MapUtil.get(map, "location", InternalPoint2D.class);
                    if (internalPoint2D != null) {
                        actorDTO.setLatitude(internalPoint2D.x());
                        actorDTO.setLongitude(internalPoint2D.y());
                    }
                    return actorDTO;
                })
                .all();
        return actorCollection;
    }

    public void deleteAllData() {
        movieRepository.deleteAllData();
    }

    public Long count() {
        return movieRepository.count();
    }
}
